<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
<title>冀银科技-管理系统</title>
<!--清理浏览器端缓存 -->
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- IE 以最高级模式渲染文档 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!--width:可视区域的宽度，值可为数字或关键词device-width,maximum-scale=1.0将禁止用户放大到实际尺寸之上-->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <link rel="stylesheet" href="${basePath}/assets/css/bootstrap.min.css${VERSION}"/>
    <link rel="stylesheet" href="${basePath}/assets/css/font-awesome.min.css${VERSION}"/>
    <link rel="stylesheet" href="${basePath}/assets/css/font-awesome-ace.min.css${VERSION}"/>

    <script src="${basePath}/assets/js/jquery.min.js${VERSION}"></script>
    <script src="${basePath}/assets/js/bootstrap.min.js${VERSION}"></script>
    <script type="text/javascript" src="${basePath}/plugins/layer/layer.js${VERSION}"></script>

</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-xs-10" style="padding-top: 20px;left: 8%;">
            <form id="form" class="form-horizontal" enctype="multipart/form-data" onsubmit="return submitHandler()">
                <div class="form-group">
                    <label for="inputFilesPath" class="col-xs-4 control-label text-right">你的上线包主路径：</label>
                    <div class="col-xs-7">
                        <input id="inputFilesPath" type="file" class="form-control"
                              name="inputFiles" webkitdirectory mozdirectory placeholder="For example: C:\**\your file's name">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-xs-4"></div>
                    <div class="col-xs-8">
                        <button id="submit" type="submit" class="btn btn-primary">Submit</button>
                    </div>
                    <div class="col-xs-10" style="left: 8%;margin-top: 15px;">
                        <p class="help-block">注：请在Google Chrome浏览器中使用, 其余浏览器可能会出现兼容性问题!</p>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="progress" style="display: none;">
    <div class="progress-bar progress-bar-success" role="progressbar"
         aria-valuenow="60" aria-valuemin="0" aria-valuemax="100"
         style="width: 90%;">
        <span class="sr-only">90% 完成（成功）</span>
    </div>
</div>

<script>
    layer.config({
        resize: false
    });
    var title = "提示信息";

    var $form = $("#form");
    var deployController = "${basePath}/deploy/deployFiles";
    var ajaxMethod = "POST";

    function submitHandler() {
        disableBtns();
        var val = $("#inputFilesPath").val();
        if(val != null && val != "" && val != "undefined"){
            var formData = new FormData($('form')[0]);
            $.ajax({
                url: deployController,
                type: ajaxMethod,
                contentType: false,//必须false才会自动加上正确的Content-Type
                processData: false,//必须false才会避开jQuery对 formdata 的默认处理XMLHttpRequest会对 formdata 进行正确的处理
                data: formData,
                dataType: "json",
                success: function (data) {
                    unDisableBtns();
                    if(data.status == '0'){
                        layer.confirm(data.data, {
                            title: title,
                            icon: 3,
                            btn: ['开始','取消'] //按钮
                        }, function(index){
                            layer.close(index);
                            deployFiles();
                        });
                    }else{
                        alertInfo(data.message);
                    }
                },
                error: function (data) {
                    unDisableBtns();
                    alertInfo("系统异常!");
                }
            });
        }else{
            unDisableBtns();
            $("#inputFilesPath").focus();
            layer.tips('包路径必填', '#inputFilesPath', {
                tips: [2, '#FF5722'],
                time: 2000
            });
        }
        return false;
    }

    function deployFiles() {
        disableBtns();
        var val = $("#inputFilesPath").val();
        if(val != null && val != "" && val != "undefined") {
            layer.prompt({title: '请输入口令，并确认', formType: 1}, function(pass, index) {
                if(isNotNull(pass)){
                    var formData = new FormData($('form')[0]);
                    formData.append("isCheck", "true");
                    formData.append("pwd", pass);
                    layer.close(index);
                    $.ajax({
                        url: deployController,
                        type: ajaxMethod,
                        contentType: false,//必须false才会自动加上正确的Content-Type
                        processData: false,//必须false才会避开jQuery对 formdata 的默认处理XMLHttpRequest会对 formdata 进行正确的处理
                        data: formData,
                        dataType: "json",
                        success: function (data) {
                            unDisableBtns();
                            if(data.status == 0){
                                successInfo(data.data);
                            }else{
                                alertInfo(data.message);
                            }
                        },
                        error: function (data) {
                            unDisableBtns();
                            errorInfo("系统异常!")
                        }
                    });
                } else {
                    alertInfo("口令错误! 已通知管理员");
                }
            },function (index) {
                unDisableBtns();
                layer.close(index);
            });
        }else{
            unDisableBtns();
            $("#inputFilesPath").focus();
            layer.tips('包路径必填', '#inputFilesPath', {
                tips: [2, '#FF5722'],
                time: 2000
            });
        }
    }

    /*是否为空*/
    function isNull(str) {
        return str == null || str == "";
    }
    /*是否不为空*/
    function isNotNull(str) {
        return !isNull(str);
    }
    /*禁用所有按钮*/
    function disableBtns() {
        $(".container :button").each(function (obj) {
            $(this).attr("disabled", "disabled");
        });
    }
    /*解开所有按钮*/
    function unDisableBtns() {
        $(".container :button").each(function (obj) {
            $(this).removeAttr("disabled");
        });
    }
    /*警告信息*/
    function alertInfo(info){
        layer.alert(info,{
            title: title,
            icon: 0,
            closeBtn: 0, //不显示关闭按钮
            anim: 4,//从左翻滚
            shadeClose: fasle, //开启遮罩关闭
        });
    }
    /*异常信息*/
    function errorInfo(info) {
        layer.alert(info,{
            title: title,
            icon: 2,
            closeBtn: 0, //不显示关闭按钮
            anim: 6,//抖动
            shadeClose: false, //开启遮罩关闭
        });
    }
    /*成功信息*/
    function successInfo(info) {
        layer.alert(info,{
            title: title,
            icon: 1,
            closeBtn: 0, //不显示关闭按钮
            anim: 0,
            shadeClose: false, //开启遮罩关闭
        });
    }

</script>

</body>
</html>
